#include "stdafx.h"
#include "ClassFactory.h"
#include "ExtAdfilter.h"
#include "TWExtAdfilter.h"

extern UINT _nDllRefCount;

CClassFactory::CClassFactory(CLSID clsid)
{
	m_clsidObject = clsid;
	m_ObjRefCount = 1;
	_nDllRefCount++;
}

CClassFactory::~CClassFactory()
{
	_nDllRefCount--;
}

STDMETHODIMP CClassFactory::QueryInterface(REFIID riid, LPVOID *ppReturn)
{
	*ppReturn = NULL;

	if(IsEqualIID(riid, IID_IUnknown))
		*ppReturn = this;
   
	else if(IsEqualIID(riid, IID_IClassFactory))
		*ppReturn = (IClassFactory*)this;

	if(*ppReturn)
	{
		(*(LPUNKNOWN*)ppReturn)->AddRef();
		return S_OK; 
	}

	return E_NOINTERFACE;
}                                             

STDMETHODIMP_(DWORD) CClassFactory::AddRef()
{
	return ++m_ObjRefCount;
}

STDMETHODIMP_(DWORD) CClassFactory::Release()
{
	if( --m_ObjRefCount == 0 )
	{
		delete this;
		return 0;
	}
   
	return m_ObjRefCount;
}

STDMETHODIMP CClassFactory::CreateInstance(  LPUNKNOWN pUnknown, 
                                             REFIID riid, 
                                             LPVOID *ppObject)
{
	HRESULT  hResult = E_FAIL;
	LPVOID   pTemp = NULL;

	*ppObject = NULL;
	//pObj;
	if(pUnknown != NULL)
		return CLASS_E_NOAGGREGATION;
	CExtAdfilterObject * pObj = new CExtAdfilterObject();
	if(NULL == pObj)
		return E_OUTOFMEMORY;
	pTemp = pObj;
 	if(pTemp)
	{
		//get the QueryInterface return for our return value
		hResult = ((LPUNKNOWN)pTemp)->QueryInterface(riid, ppObject);
		//call Release to decement the ref count
		((LPUNKNOWN)pTemp)->Release();
	}

	return hResult;
}

STDMETHODIMP CClassFactory::LockServer(BOOL)
{
	return E_NOTIMPL;
}

